package com.chenchao.javafile;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.EmptyFileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.SuffixFileFilter;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/21 10:02
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javafile.Test18.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * commonIOTest
 */
public class Test18 {
    public static void main(String[] args) throws IOException {
        //test01();
        //test02();
        //test03();
        //test04();
        test05();

    }

    /**
     * 统计大小
     */
    public static void test01(){
        File dir = new File("myjava01/src/fo");
        File file = new File("myjava01/src/fo/a2.txt");
        //目录大小
        long len1= FileUtils.sizeOf(dir);
        System.out.println(len1);

        //文件大小
        long len2=FileUtils.sizeOf(file);
        System.out.println(len2);
    }

    /**
     * 遍历子孙级listFiles()
     */
    public static void test02(){
        File dir = new File("myjava01/src");
        Collection<File> files =
                FileUtils.listFiles(dir, EmptyFileFilter.NOT_EMPTY, null);
        //三个参数分别为要遍历的目录，过滤器，目录过滤器，默认是遍历一层目录
        for (File file : files) {
            System.out.println(file.getName());
        }


        Collection<File> files2 =
                FileUtils.listFiles(dir, EmptyFileFilter.NOT_EMPTY, DirectoryFileFilter.INSTANCE);
        //遍历所有子孙级别
        for (File file : files2) {
            System.out.println(file.getName());
        }

        System.out.println("==========");
        Collection<File> files3 =FileUtils.listFiles(dir, FileFilterUtils.or(new SuffixFileFilter(".java"),
                new SuffixFileFilter(".txt")), DirectoryFileFilter.INSTANCE);
        //or 以.java与.txt结尾的
        for (File file : files3) {
            System.out.println(file.getName());
        }

        System.out.println("==========");
        Collection<File> files4 =FileUtils.listFiles(dir, FileFilterUtils.and(EmptyFileFilter.NOT_EMPTY,
                new SuffixFileFilter(".txt")), DirectoryFileFilter.INSTANCE);
        //and 以txt结尾的不为空的
        for (File file : files4) {
            System.out.println(file.getName());
        }
    }

    /**
     * 读内容
     */
    public static void test03() throws IOException {
        File file = new File("myjava01/src/fo/aa.txt");
        String s = FileUtils.readFileToString(file,"UTF-8");
        System.out.println(s);
        //可以跨行读写
        byte[] b=FileUtils.readFileToByteArray(file);
        System.out.println(b.length);

        //逐行读取
        List<String> strings = FileUtils.readLines(file,"UTF-8");
        for (String string : strings) {
            System.out.println(string);
        }

        System.out.println("===============");
        //迭代器读取每行
        LineIterator lineIterator = FileUtils.lineIterator(file, "UTF-8");
        String str1;
        while(lineIterator.hasNext()){
            str1=lineIterator.nextLine();
            System.out.println(str1);
        }


    }
    /**
     * 写内容
     */
    public static void test04() throws IOException {
        File file = new File("myjava01/src/fo/aa.txt");
        FileUtils.write(file,"最后一个参数表示是否追加文件\r\n","UTF-8",true);
        //不会自动换行
        FileUtils.writeStringToFile(file,"最后一个参数表示是否追加文件\r\n","UTF-8",true);
        FileUtils.writeByteArrayToFile(file,"最后一个参数表示是否追加文件\r\n".getBytes(StandardCharsets.UTF_8),true);
        //三种写法都可以

        //写入字符串列表
        ArrayList<String> strings = new ArrayList<>();
        strings.add("apple");
        strings.add("香蕉");
        strings.add("pear");
        strings.add("葡萄");
        FileUtils.writeLines(file,"UTF-8",strings,"是水果",true);
        //第三个参数是每个元素的连接字符串
    }

    /**
     * 拷贝文件
     */
    public static void test05() throws IOException {
        //拷贝文件到文件
        File file = new File("myjava01/src/fo/aa.txt");
        File copyFile = new File("myjava01/src/fo/aa_01.txt");
        FileUtils.copyFile(file,copyFile);

        File dir = new File("myjava01/src/fo/test");
        //拷贝文件到文件夹
        FileUtils.copyFileToDirectory(file,dir);

        //拷贝文件夹到文件夹
        File dir2 = new File("myjava01/src/fo/img");
        FileUtils.copyDirectoryToDirectory(dir,dir2);

        //拷贝目录里面的所有子文件
        FileUtils.copyDirectoryToDirectory(dir,dir2);

        //拷贝URL内容
        FileUtils.copyURLToFile(new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605939274369&di=4109a55ebebd7711576f7d1766203b76&imgtype=0&src=http%3A%2F%2Fimg.aiimg.com%2Fuploads%2Fuserup%2F0909%2F2Z64022L38.jpg"),
                new File(dir2,"tt.jpg"));
        //读取网页
        String s = IOUtils.toString(new URL("http://www.baidu.com"), StandardCharsets.UTF_8);
        System.out.println(s);


    }
}
